package ru.jarda.model.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.jarda.model.entities.Operation;

import java.util.List;


/**
 * Created by User on 09.08.2015.
 */


@Component
@Transactional
public class OperationDaoImpl implements Dao<Operation> {
    @Autowired
    SessionFactory sessionFactory;

    @Override
    public void saveObject(Operation operation) {
        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(operation);
    }

    @Override
    public void deleteObject(Number id) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(new Operation(id));
    }

    public void deleteObject(Operation operation) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(operation);
    }

    @Override
    public Operation getObject(Number id) {
        Session session = sessionFactory.getCurrentSession();
        return (Operation)session.get(Operation.class,id);
    }


    @Override
    public List<Operation> getAllObjects() {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria=session.createCriteria(Operation.class);
        criteria.add( Restrictions.eq("userId", new Operation().getUserId()));
        return criteria.list();
    }

    public void editObject(Operation o) {

    }
}
